package cc.uncarbon.module.adminapi.web.custom;

import cc.uncarbon.framework.core.constant.HelioConstant;
import cc.uncarbon.framework.core.page.PageParam;
import cc.uncarbon.framework.core.page.PageResult;
import cc.uncarbon.framework.web.model.request.IdsDTO;
import cc.uncarbon.framework.web.model.response.ApiResult;
import cc.uncarbon.module.adminapi.constant.AdminApiConstant;
import cc.uncarbon.module.adminapi.util.AdminStpUtil;
import cc.uncarbon.module.custom.model.request.AdminStaffInsertOrUpdateDTO;
import cc.uncarbon.module.custom.model.request.AdminStaffListDTO;
import cc.uncarbon.module.custom.model.response.StaffBO;
import cc.uncarbon.module.custom.service.StaffService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 后台管理-技师管理接口
 */
@SaCheckLogin(type = AdminStpUtil.TYPE)
@Tag(name = "技师管理接口")
@RequestMapping(AdminApiConstant.HTTP_API_URL_PREFIX + "/api/v1")
@RequiredArgsConstructor
@RestController
@Slf4j
public class AdminStaffController {

    // 功能权限串前缀
    private static final String PERMISSION_PREFIX = "Staff:";

    private final StaffService staffService;


    @SaCheckPermission(type = AdminStpUtil.TYPE, value = PERMISSION_PREFIX + HelioConstant.Permission.RETRIEVE)
    @Operation(summary = "分页列表")
    @GetMapping("/staffs")
    public ApiResult<PageResult<StaffBO>> list(PageParam pageParam, AdminStaffListDTO dto) {
        return ApiResult.data(staffService.adminList(pageParam, dto));
    }

    @SaCheckPermission(type = AdminStpUtil.TYPE, value = PERMISSION_PREFIX + HelioConstant.Permission.RETRIEVE)
    @Operation(summary = "详情")
    @GetMapping(value = "/staffs/{id}")
    public ApiResult<StaffBO> getById(@PathVariable Long id) {
        return ApiResult.data(staffService.getOneById(id, true));
    }

    @SaCheckPermission(type = AdminStpUtil.TYPE, value = PERMISSION_PREFIX + HelioConstant.Permission.CREATE)
    @Operation(summary = "新增")
    @PostMapping("/staffs")
    public ApiResult<Void> insert(@RequestBody @Valid AdminStaffInsertOrUpdateDTO dto) {
        staffService.adminInsert(dto);
        return ApiResult.success();
    }

    @SaCheckPermission(type = AdminStpUtil.TYPE, value = PERMISSION_PREFIX + HelioConstant.Permission.UPDATE)
    @Operation(summary = "编辑")
    @PutMapping(value = "/staffs/{id}")
    public ApiResult<Void> update(@PathVariable Long id, @RequestBody @Valid AdminStaffInsertOrUpdateDTO dto) {
        dto.setId(id);
        staffService.adminUpdate(dto);
        return ApiResult.success();
    }

    @SaCheckPermission(type = AdminStpUtil.TYPE, value = PERMISSION_PREFIX + HelioConstant.Permission.DELETE)
    @Operation(summary = "删除")
    @DeleteMapping("/staffs")
    public ApiResult<Void> delete(@RequestBody @Valid IdsDTO<Long> dto) {
        staffService.adminDelete(dto.getIds());
        return ApiResult.success();
    }

}
